package local.blueheart.data.springdatademo.domino;

import lotus.domino.Base;
import lotus.domino.NotesException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Set;
import java.util.Vector;

/**
 * Domino对象的包装基类
 */
public class DBase {

    private static final Logger LOGGER = LoggerFactory.getLogger(DBase.class);

    private Base base;

    private Set<DBase> childs = new HashSet<>();

    public DBase(){}

    public DBase(Base param){
        this.base = param;
    }

    public Base getBase() {
        return base;
    }

    public void setBase(Base base) {
        this.base = base;
    }

    public void addChild(DBase base){
        childs.add(base);
    }

    public void delChild(DBase base){
        childs.remove(base);
    }

    public void recycle(){
        //清除子
        childs.forEach( dbase -> {
            if(dbase!=null && dbase.base!=null){
                try {
                    dbase.base.recycle();
                } catch (NotesException e) {
                    LOGGER.error("base recycle failure",e);
                }
            }
        });
        //清除自己
        if(base!=null){
            try {
                base.recycle();
            } catch (NotesException e) {
               LOGGER.error("base recycle failure",e);
            }
        }
    }


}
